package com.leyou.controller;

import com.leyou.dto.SpecGroupDTO;
import com.leyou.dto.SpecParamDTO;
import com.leyou.pojo.SpecGroup;
import com.leyou.pojo.SpecParam;
import com.leyou.service.SpecGroupService;
import com.leyou.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jshstart
 * @creat 2020-12-11 13:00
 */
@RestController
@RequestMapping("/spec")
public class SpecParamController {
    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据条件查找规格参数
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamDTOByCondition(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId",required = false) Long groupId,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){
        List<SpecParamDTO> list=specParamService.querySpecParamDTOByCondition(categoryId,groupId,searching);
        return ResponseEntity.ok(list);
    }
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO){

        specParamService.save(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.ok().build();
    }
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO){

        specParamService.updateById(specParamDTO.toEntity(SpecParam.class));
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/param/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable("id") Long id){
        System.out.println("id = " + id);
        specParamService.removeById(id);
        return ResponseEntity.ok().build();
    }
}
